﻿//*************************************************************************
//面向服务的协作式智能交通系统仿真软件SoC-ITSS v2.1 alpha
//发布单位：西北工业大学--巴黎高科MINES机器人与群智能系统联合实验室
//团队负责人：张凯龙 Zhang Kailong
//团队成员：张大方、杨安胜、王敏、谢策、王雨佳、谢尘玉等
//联系我们：kl.zhang@nwpu.edu.cn
//2012-2021
//*************************************************************************



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using CyberCarSimulation.Bean;
using CyberCarSimulation.Interface;
using CyberCarSimulation.Utils;
using CyberCarSimulation.Simulation;
using CyberCarSimulation.RoadModel;
using System.Xml;

//*******************************************************
//
//文件名(File Name):           ConfigTool.cs
//
//数据表(Tables):              nothing 
//
//作者(Author):                谢策
//
//日期(Create Date):           2017.7.8
//
//版本(Version):               Oreo
//
//版本号(VersionID):           3.0
//
//修改记录(Revision History):
//
//      R1:
//          修改作者：         谢策
//          修改日期：         2017.7.9
//          修改理由：         增加界面控件
//
//      R2:
//          修改作者：         谢策
//          修改日期：         2017.12.7
//          修改理由：         修改导入地图信息
//
//*******************************************************
namespace CyberCarSimulation
{
    /// <summary>
    /// 配置参数界面
    /// </summary>
    public partial class ConfigTool : Form
    {
        
        public ConfigTool()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 检查输入是否正确,如果控件内数据合格，将数据放在ConfigurationParameter中
        /// </summary>
        /// <returns></returns>
        private bool CheckInput()
        {
            bool flag = true;
            if (this.TxtVehicleDensity.Text == "" || this.TxtReservationLineLength.Text == "" || this.TxtTestTime.Text == ""
                || this.TxtPercentageBig.Text == "" || this.TxtMaximumSpeed.Text == "" || this.TxtMaximumAcceleration.Text == ""
                || this.TxtMaximumDeceleration.Text == "" || this.TxtCruisingSpeed.Text == "" || this.TxtCrossingSpeed.Text == ""
                || this.TxtVehicleQuantity.Text == "" || this.TxtHighPriorPercentage.Text == "" || this.TxtMidPriorPercentage.Text == "")
            {
                MessageBox.Show("Please enter the correct configuration parameters！");
                flag = false;
            }
            else
            {
                //将数据存入全局的数据变量：软件配置中
                MainData.SimulationConfig = new SimulationConfig(float.Parse(this.TxtVehicleDensity.Text), float.Parse(this.TxtReservationLineLength.Text), int.Parse(this.TxtTestTime.Text),
                    float.Parse(this.TxtPercentageBig.Text), float.Parse(this.TxtMaximumSpeed.Text), float.Parse(this.TxtMaximumAcceleration.Text),
                    float.Parse(this.TxtMaximumDeceleration.Text), float.Parse(this.TxtCruisingSpeed.Text), float.Parse(this.TxtCrossingSpeed.Text),
                    int.Parse(this.TxtVehicleQuantity.Text), float.Parse(this.TxtHighPriorPercentage.Text), float.Parse(this.TxtMidPriorPercentage.Text),
                    float.Parse(this.TxtServiceRatio.Text), float.Parse(this.TxtJamRatio.Text), float.Parse(this.TxtTimeRatio.Text));
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 导入软件运行的XML配置文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImportConfiguration(object sender, EventArgs e)
        {
            //显示OpenConfigDialog
            this.OpenConfigDialog.FileName = null;
            this.OpenConfigDialog.ShowDialog();
            string FilePath = this.OpenConfigDialog.FileName;
            if (FilePath == "")
            {
                return;
            }

            //读取XML数据
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(FilePath);

            //解析config.xml数据
            XmlNode root = XmlDoc.SelectSingleNode("ConfigList");
            XmlNodeList nodeList = root.ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                if ("VehicleDensity".Equals(xn.Name))
                {
                    this.TxtVehicleDensity.Text = xn.InnerText;
                }
                else if ("ReservationLineLength".Equals(xn.Name))
                {
                    this.TxtReservationLineLength.Text = xn.InnerText;
                }
                else if("TestTime".Equals(xn.Name)){
                    this.TxtTestTime.Text = xn.InnerText;
                }
                else if ("PercentageBig".Equals(xn.Name))
                {
                    this.TxtPercentageBig.Text = xn.InnerText;
                }
                else if ("MaximumSpeed".Equals(xn.Name))
                {
                    this.TxtMaximumSpeed.Text = xn.InnerText;
                }
                else if ("MaximumAcceleration".Equals(xn.Name))
                {
                    this.TxtMaximumAcceleration.Text = xn.InnerText;
                }
                else if ("MaximumDeceleration".Equals(xn.Name))
                {
                    this.TxtMaximumDeceleration.Text = xn.InnerText;
                }
                else if ("CruisingSpeed".Equals(xn.Name))
                {
                    this.TxtCruisingSpeed.Text = xn.InnerText;
                }
                else if ("CrossingSpeed".Equals(xn.Name))
                {
                    this.TxtCrossingSpeed.Text = xn.InnerText;
                }
                else if ("VehicleQuantity".Equals(xn.Name))
                {
                    this.TxtVehicleQuantity.Text = xn.InnerText;
                }
                else if ("HighPriorPercentage".Equals(xn.Name))
                {
                    this.TxtHighPriorPercentage.Text = xn.InnerText;
                }
                else if ("MidPriorPercentage".Equals(xn.Name))
                {
                    this.TxtMidPriorPercentage.Text = xn.InnerText;
                }
                else if ("ServiceRatio".Equals(xn.Name)) 
                {
                    this.TxtServiceRatio.Text = xn.InnerText;
                }
                else if ("JamRatio".Equals(xn.Name))
                {
                    this.TxtJamRatio.Text = xn.InnerText;
                }
                else if ("TimeRatio".Equals(xn.Name))
                {
                    this.TxtTimeRatio.Text = xn.InnerText;
                }
            }

            //检测输入是否合法
            if (!CheckInput())
            {
                return;
            }
        }


        /// <summary>
        /// 导入地图背景图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImportMap(object sender, EventArgs e)
        {
            //显示OpenConfigDialog
            this.OpenMapDialog.FileName = null;
            this.OpenMapDialog.ShowDialog();

            //XML地图信息文件的路径
            string FilePath = this.OpenMapDialog.FileName;
            if (FilePath == "")
            {
                return;
            }

            //设置地图背景文件路径
            MainData.Map.SetMapImageURL(FilePath);

        }

        /// <summary>
        /// 导入XML的地图信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImportInf(object sender, EventArgs e)
        {
            //显示OpenConfigDialog
            this.OpenInfDialog.FileName = null;
            this.OpenInfDialog.ShowDialog();

            //XML地图信息文件的路径
            string FilePath = this.OpenInfDialog.FileName;
            if (FilePath == "")
            {
                return;
            }

            //调用LoadXML类解析XML地图信息文件
            LoadXML LoadXML = new LoadXML();
            LoadXML.LoadMapInformation(FilePath);

            //对模型中数据处理,方便仿真时数据使用
            ModelHandle.DataHandle();
            
        }

        /// <summary>
        /// 开始软件仿真
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartSimulation(object sender, EventArgs e)
        {
            ////Test Code
            //new About().Show();

            //检测输入是否合法，同时更新ConfigurationParameter
            if (!CheckInput())
            {
                return;
            }
            //进入路口配置界面
            this.Hide();
            //ConfigMap ConfigRoad = new ConfigMap();
            //ConfigRoad.Show();
            new DisplayForm().Show();
        }

    }
}
